using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace CodePlex.OpenSearch
{
    internal class SearchHandlerSettings 
    {

        private readonly OpenSearchDescription _description;

        private string _httpBase;
        private string _descriptionPath;
        private string _suggestPath;
        private string _searchPath;

        private string _imagePng64Path;
        private string _imageIco16Path;
        private Func<Stream> _imagePng64Source;
        private Func<Stream> _imageIco16Source;

        internal SearchHandlerSettings(OpenSearchDescription description)
        {
            _description = description;
        }

        internal OpenSearchDescription OpenSearchServerDescription
        {
            get
            {
                return _description;
            }
        }

        internal string SuggestPath
        {
            get
            {
                return _suggestPath;
            }

            set
            {
                _suggestPath = value;

                this.Sync();
            }
        }

        internal string DescriptionPath
        {
            get
            {
                return _descriptionPath;
            }

            set
            {
                _descriptionPath = value;

                this.Sync();
            }
        }

        internal string SearchPath
        {
            get
            {
                return _searchPath;
            }

            set
            {
                _searchPath = value;

                this.Sync();
            }
        }



        internal string HttpBase
        {
            get
            {
                return _httpBase;
            }

            set
            {
                _httpBase = value;

                this.Sync();
            }
        }



        internal string ImagePng64Path
        {
            get
            {
                return _imagePng64Path;
            }

            set
            {
                _imagePng64Path = value;

                this.Sync();
            }
        }

        internal string ImageIco16Path
        {
            get
            {
                return _imageIco16Path;
            }

            set
            {
                _imageIco16Path = value;

                this.Sync();
            }
        }


        internal Func<Stream> ImagePng64Source
        {
            get
            {
                return _imagePng64Source;
            }

            set
            {
                _imagePng64Source = value;

              
            }
        }

        internal Func<Stream> ImageIco16Source
        {
            get
            {
                return _imageIco16Source;
            }

            set
            {
                _imageIco16Source = value;


            }
        }

      
        private void Sync()
        {
          


            // mozilla stuff
            this.OpenSearchServerDescription.SearchFormUrl = string.Format("{0}{1}",this.HttpBase,this.SearchPath);

          

            // search urls
            this.OpenSearchServerDescription.Urls.Clear();

            this.OpenSearchServerDescription.Urls.Add(new Url(Constants.MimeTypes.TextHtml,
                 string.Format("{0}{1}?q={{searchTerms}}&pw={{startPage?}}", this.HttpBase, this.SearchPath)));

            this.OpenSearchServerDescription.Urls.Add(new Url(Constants.MimeTypes.ApplicationRssXml,
             string.Format("{0}{1}?q={{searchTerms}}&pw={{startPage?}}&format=rss", this.HttpBase, this.SearchPath)));

            this.OpenSearchServerDescription.Urls.Add(new Url(Constants.MimeTypes.ApplicationAtomXml,
            string.Format("{0}{1}?q={{searchTerms}}&pw={{startPage?}}&format=atom", this.HttpBase, this.SearchPath)));





            this.OpenSearchServerDescription.Urls.Add(new Url(Constants.MimeTypes.ApplicationXSuggestionsJson,
               string.Format("{0}{1}?q={{searchTerms}}", this.HttpBase, this.SuggestPath)));





            // images

            this.OpenSearchServerDescription.Images.Clear();

            this.OpenSearchServerDescription.Images.Add(new Image(64, 64, Constants.MimeTypes.ImagePng, 
                string.Format("{0}{1}", this.HttpBase, this.ImagePng64Path)));
            this.OpenSearchServerDescription.Images.Add(new Image(16, 16, Constants.MimeTypes.ImageVndMicrosoftIcon,
                string.Format("{0}{1}", this.HttpBase, this.ImageIco16Path)));

        }



        internal string DescriptionUrl
        {
            get
            {
                return string.Format("{0}{1}", this.HttpBase,this.DescriptionPath);
            }
        }



       
    }
}
